// pages/view/cl/drugConsumption/drugConsumption.js
const network = require('../../../../utils/network.js')
var wxCharts = require('../../../../utils/wxcharts.js');
var app = getApp();
var columnChart = null;
var lineChart = null;


Page({
  data: {
    chartTitle: '药品消耗趋势（数量）',
    isMainChartDisplay: true,
    categories : [0,0,0,0,0],
    amount_data : [0,0,0,0,0],
    quantity_data : [0,0,0,0,0]

  },


  onLoad: function(options){
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    that.myRequest(options)
  
  },
  /**
   * 调用封装好的网络请求，获取数据
   */
  myRequest: function (options) {
    var that = this
    console.log('options:', options)
    //调用封装好的网络请求，获取数据
    network.get('/cl/drugconsumption?startTime=' + options.startTime + '&endTime=' + options.endTime + '&itemId=' + options.itemId, {}
    ).then(function (res) {
      //console.log('res:', res)
      var obj = res.data.statusCode
      var amount_obj = res.data.data_2.data_1
      var quantity_obj = res.data.data_2.data_2
      var time_obj = res.data.data_2.data_3

      if (obj === 100) {
        wx.showModal({

          title: '提示',

          content: '所选日期数据为空，请重新选择日期！',

        })
      } else {
        that.setData({
          'quantity_data': quantity_obj,
          'categories': time_obj,
          'amount_data': amount_obj
        })
      }
    }).then(function (res) {
      wx.hideLoading()
      that.createColumnChart();
      that.createlineChart();
    }).catch(function (res) {
      console.log(res)
    })
  },

  createColumnChart: function(){
    var that = this
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: that.data.categories,
      series: [{
        name: '消耗数量',
        data: that.data.quantity_data,
        format: function (val, name) {
          return val.toFixed(2) + '粒';
        }
      }],
      yAxis: {
        format: function (val) {
          return val + '粒';
        },
        title: '消耗数量',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
    });

  },
  createlineChart: function(){
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    //var simulationData = this.createSimulationData();
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: that.data.categories,
      animation: true,
      // background: '#f5f5f5',
      series: [{
        name: '药品消耗趋势',
        data: that.data.amount_data,
        format: function (val, name) {
          return val.toFixed(2) + '元';
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '消耗金额 (元)',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: windowWidth,
      height: 200,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  }
});